package com.strawcom.charmring.bean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.litepal.crud.DataSupport;

import com.strawcom.charmring.App;
import com.strawcom.charmring.C;
import com.strawcom.charmring.util.FormatUtil;
import com.strawcom.charmring.util.LogUtil;

/**
 * @author WangPeng
 */
public class WeeklySummary {

	private WeeklySummary() {
	}

	private static WeeklySummary weeklySummary = new WeeklySummary();
	public final List<WeekSummary> weekSummaryList = new ArrayList<WeeklySummary.WeekSummary>();

	public static WeeklySummary getInstance() {
		return weeklySummary;
	}

	public static class WeekSummary {
		public final List<DailySummary> dailySummaryList = new ArrayList<DailySummary>();

		public String getTitle() {
			String first = dailySummaryList.get(0).getDate();
			String last = dailySummaryList.get(dailySummaryList.size() - 1).getDate();
			StringBuffer sb = new StringBuffer();
			sb.append(FormatUtil.formatDate(first, C.exercise.record_date_pattern, FormatUtil.MM_dd_));
			sb.append("~");
			sb.append(FormatUtil.formatDate(last, C.exercise.record_date_pattern, FormatUtil.MM_dd_));
			return sb.toString();
		}
	}
	
	private DailySummary getDailySummary(Date date) {
		String d = FormatUtil.formatDate(date, C.exercise.record_date_pattern);
		List<DailySummary> dailySummaryList = DataSupport.where("date = ?", d).find(DailySummary.class);
		if (dailySummaryList != null && dailySummaryList.size() > 0) {
			return dailySummaryList.get(0);
		}
		else {
			return DailySummary.newEmptyIntance(App.i(), d);
		}
	}

	public void handleSummaryInfo() {
		List<DailySummary> dailySummaryList = DataSupport.findAll(DailySummary.class);
		if(dailySummaryList == null || dailySummaryList.size() < 1){
			//dailySummaryList.add(DailySummary.newIntance(App.i()));
			return;
		}

		Calendar c = Calendar.getInstance();
		Date firstDate = FormatUtil.parseDate(dailySummaryList.get(0).getDate(), C.exercise.record_date_pattern);
		c.setTime(firstDate);
		int firstIndex = c.get(Calendar.DAY_OF_WEEK);
		final Date startDate = new Date(firstDate.getTime() - (firstIndex -1)* 24 * 3600000);
		Date lastDate = FormatUtil.parseDate(dailySummaryList.get(dailySummaryList.size() - 1).getDate(), C.exercise.record_date_pattern);
		c.setTime(lastDate);
		int lastIndex = c.get(Calendar.DAY_OF_WEEK);
		int count = firstIndex + (7- lastIndex) + FormatUtil.timeSpcaing(firstDate, lastDate);
//		LogUtil.e("WeeklySummary handleSummaryInfo() \ncount:" + count);
		
		dailySummaryList.clear();
		Date tmp = startDate;
		for (int i = 0; i < count; i++) {
			DailySummary ds = getDailySummary(tmp);
			dailySummaryList.add(ds);
			
			tmp = new Date(tmp.getTime() +  24 * 3600000);
			
//			LogUtil.e("ds("+i+") time:" + (tmp.getTime() ));
//			LogUtil.e("ds("+i+") BBBdate: " +tmp+ "    AAAdate:"+ ds.getDate());
		}
		
		weekSummaryList.clear();
		for (int i = 0; i < dailySummaryList.size(); i = i + 7) {
			WeekSummary weekSummary = new WeekSummary();
			weekSummary.dailySummaryList.addAll(dailySummaryList.subList(i, i + 7));
			weekSummaryList.add(weekSummary);
		}
	}

}
